import { contextBridge, ipcRenderer } from "electron/renderer";
import {ViewName} from "./type/const.js";
import {HttpGetReq, idReq, SourceRsp} from "./type/interfaces.js";

const apiKey = "electron";

export type ElectronApi = typeof electronApi;

const electronApi = {
  openWorkspace(){
    return ipcRenderer.invoke("openWorkspace");
  },
  openUrl(url: string) {
    return ipcRenderer.invoke("openUrl", url);
  },
  saveConfig(data: string) {
    return ipcRenderer.invoke("saveConfig", data);
  },
  getConfig(data: string) {
    return ipcRenderer.invoke("getConfig", data);
  },
  getMachineId(){
    return ipcRenderer.invoke("getMachineId");
  },
  getOsInfo(){
    return ipcRenderer.invoke("getOsInfo");
  },
  httpGet(req: HttpGetReq){
    return ipcRenderer.invoke("httpGet", req);
  },
  getStream(req: HttpGetReq){
    return ipcRenderer.invoke("getStream", req);
  },
  abortStream(req: idReq){
    return ipcRenderer.invoke("abortStream", req);
  },
  showMenu(){
    return ipcRenderer.invoke("showMenu");
  },
  playVideo(e: SourceRsp){
    return ipcRenderer.invoke("playVideo", e);
  },
  clearUrlCache(){
    return ipcRenderer.invoke("clearUrlCache");
  },
  openViewDevTool(vType: ViewName) {
    return ipcRenderer.invoke("openViewDevTool", vType);
  },
  viewShow(vType: ViewName) {
    return ipcRenderer.invoke("viewShow", vType);
  },

  viewHide(vType: ViewName) {
    return ipcRenderer.invoke("viewHide", vType);
  },
  closeViewPage(vType: ViewName) {
    return ipcRenderer.invoke("closeViewPage", vType);
  },
  viewGoBack(vType: ViewName) {
    return ipcRenderer.invoke("viewGoBack", vType);
  },
  viewReload(vType: ViewName) {
    return ipcRenderer.invoke("viewReload", vType);
  },
  viewLoadUrl(url: string, vType: ViewName) {
    return ipcRenderer.invoke("viewLoadUrl", url, vType);
  },
  setViewBounds(bounds: Electron.Rectangle, vType: ViewName) {
    return ipcRenderer.invoke("setViewBounds", bounds, vType);
  },
  addIpcListener(channel: string, listener: any): void {
    ipcRenderer.on(channel, listener);
  },
  delIpcListener(channel: string, listener: any): void {
    ipcRenderer.removeListener(channel, listener);
  },
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
